package com.example.xd.mvp_project.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.example.xd.mvp_project.R;

/**
 * @auther XuDan on 2018/10/22.
 */
public class SimpleSideBar extends View {
    // 索引字母数组
    private String[] alphabet = {
            "A", "B", "C", "D", "E", "F",
            "G", "H", "I", "J", "K", "L",
            "M", "N", "O", "P", "Q", "R",
            "S", "T", "U", "V", "W", "X",
            "Y", "Z", "#"
    };

    // 当前选择的索引字母的下标
    private int currentChoosenAlphabetIndex = -1;
    // 画笔
    private Paint mPaint = new Paint();

    // 索引字母绘制大小
    private int alphabetTextSize = 20;

    public SimpleSideBar(Context context) {
        this(context, null);
    }

    public SimpleSideBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public SimpleSideBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int viewHeight = getHeight();
        int viewWidth = getWidth();
        int heightPerApphabet = viewHeight / alphabet.length;
        for (int i = 0; i < alphabet.length; i++) {
            mPaint.setColor(Color.BLACK);
            mPaint.setTypeface(Typeface.DEFAULT);
            mPaint.setTextSize(alphabetTextSize);
            mPaint.setAntiAlias(true);
            if (currentChoosenAlphabetIndex == i) {
                mPaint.setColor(Color.YELLOW);
                mPaint.setTextSize(alphabetTextSize);
                mPaint.setFakeBoldText(true);
            }
            float xPos = viewWidth / 2 - mPaint.measureText(alphabet[i]) / 2;
            int yPos = heightPerApphabet * i + heightPerApphabet;
            canvas.drawText(alphabet[i], xPos, yPos, mPaint);
            mPaint.reset();

        }
    }

    public interface OnLetterTouchedChangeListener {
        void onTouchedLetterChange(String letterTouched);
    }

    OnLetterTouchedChangeListener onLetterTouchedChangeListener;

    public void setOnLetterTouchedChangeListener(OnLetterTouchedChangeListener onLetterTouchedChangeListener) {
        this.onLetterTouchedChangeListener = onLetterTouchedChangeListener;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int action = event.getAction();
        float touchYPos = event.getY();
        int currentTouchIndex = (int) (touchYPos / getHeight() * alphabet.length);
        switch (action) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                setBackgroundResource(R.color.color_fd9f6a);
                currentChoosenAlphabetIndex = currentTouchIndex;
                if (onLetterTouchedChangeListener != null && currentTouchIndex < alphabet.length && currentTouchIndex > -1) {
                    onLetterTouchedChangeListener.onTouchedLetterChange(alphabet[currentTouchIndex]);
                }
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return true;


    }

    /*设置当前按下的是那个字母*/
    public void setTouchIndex(String word) {
        for (int i = 0; i < alphabet.length; i++) {
            if (alphabet[i].equals(word)) {
                currentChoosenAlphabetIndex = i;
                invalidate();
                return;
            }
        }
    }
}
